<template>
	<view class="publish">
		<image :src="url+tsBg" mode="" class="bg"></image>
		<view class="main_body">
			<!-- <view class="character">
				<input v-model="obj.title" type="text" placeholder="请输入作品标题 (最多18个字符)">
			</view> -->
			<view class="textarea">
				<textarea v-model="obj.content" placeholder="请输入反馈内容..."></textarea>
			</view>
			<!-- 图片上传 -->
			<view class="Upload_module">
				<view class="layout">
					<view class="picture_list">
						<!-- 图片 -->
						<view v-for="item,index in obj.chooseImags">
							<view @tap="handlePrevewImage(item)" class="cu-avatar view"
								:style="{'backgroundImage':'url('+item+')' }">
							</view>
							<image class="delete" @tap="bandleRemoveImg(index)" mode="widthFix"
								src="../../static/img/img/del1.png"></image>
						</view>

						<!-- 上传按钮 -->
						<view class="uploadImg" @click="uploadImg" v-if="obj.chooseImags.length<1">
							<view class="image">
								<image src="../../static/img/img/jiahao.png" mode="widthFix">
								</image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="color: red;font-size: 22rpx;margin-left: 20rpx;margin-top: 10rpx;">此投诉为本小程序自有投诉渠道，非微信官方投诉渠道</view>



		<!-- <view style="height: 150rpx;"></view> -->
		<view class="btn_bottom">
			<button class="cu-btn bnt2" @click="releaseBtn">提交投诉</button>
		</view>
	</view>
</template>

<script>
	import {userInfo,getComplaint} from '@/api/api.js'
	import config from '@/config'
	export default {
		data() {
			return {
				url:config.baseUrl,
				userInfo:{},
				//图片列表
				obj: {
					content: "",
					chooseImags: []
				},
				tsBg:"/uploads/web/tsBg.png",
				show: false,
				imagesList:[],
				tempFilePathList:[],
			}
		},
		watch: {
			obj: {
				deep: true, //true为进行深度监听,false为不进行深度监听
				handler(newVal) {
					console.log(newVal);
					if (newVal.title && newVal.content && newVal.chooseImags.length) {
						this.show = true;
					} else {
						this.show = false;
					}
				}
			}
		},
		onLoad() {
			this.getUser()
		},
		methods: {
			getUser(){
				let that = this
				uni.getStorage({
				    key: 'data1',
				    success: function (res) {
						// that.userInfo = res.data
						userInfo(res.data.phone).then((userRes) => {
							// let that = this
							that.userInfo = userRes.info
						  })
				    }
				});
			},
			// 上传图片
			uploadImg() {
				uni.chooseImage({
					//同时选中的图片的数量
					count: 1,
					//图片的格式  原图  压缩
					sizeType: ['original', 'compressed'],
					//图片的来源  相册  照相机
					sourceType: ['album', 'camera'],
					success: (result) => {
						console.log(result)
						let tempFilePaths = result.tempFilePaths;
						this.tempFilePathList = result.tempFilePaths;
						    let base64List = this.obj.chooseImags;
						    tempFilePaths.forEach((item, index) => {
						      uni.getFileSystemManager().readFile({
								  filePath: item, // 图片文件路径
								  encoding: 'base64', // 编码格式为base64
								  success: res => { // 成功回调
								  console.log('resresres',res)
									let base64 = 'data:image/jpeg;base64,' + res.data; // 拼接data URI
									console.log('base64base64base64',base64)
									base64List.push(base64);
									if (base64List.length === tempFilePaths.length) {
										this.obj.chooseImags = base64List;
									}
								  },
								  fail: err => {
									console.error('读取文件失败', err);
								  }
								});
						    });
					},
				});
			},
			//反馈,图片放大预览
			handlePrevewImage(e) {
				//先构造要预览的图标数组
				const urls = this.obj.chooseImags;
				//接收传递过来的图片url
				const current = e;
				wx.previewImage({
					current, // 当前显示图片的http链接
					urls // 需要预览的图片http链接数组列表
				})
			},

			//删除data里面图片的方法
			bandleRemoveImg(index) {
				console.log(index)
				//2.获取data中的图片数组
				const chooseImagsData = this.obj.chooseImags;
				//3.根据索引删除元素
				chooseImagsData.splice(index, 1);
				//4.重新填充会chooseImags中
				this.obj.chooseImags = chooseImagsData;
			},
			
			// 提交反馈
			releaseBtn(){
				if(!this.obj.content){
					uni.showToast({
						title: '请输入投诉内容',
						icon: 'none',
					});
				}else if(this.obj.chooseImags.length>0){
					this.getScImg(this.tempFilePathList)
					// this.getTsxx()
				}else{
					this.getTsxx()
				}
			},
			// 上传投诉信息
			getTsxx(imagesList){
				let data = {
					fkrName:this.userInfo.userName,
					fkrId:this.userInfo.id,
					fkrPhone:this.userInfo.phone,
					images:imagesList,
					content:this.obj.content,
					state:0,
				}
				getComplaint(data).then((res) => {
					if(res.code==200){
						uni.showToast({
							title: res.message,
							icon: 'success',
						});
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/user/user'
							 });
						  }, 1000);
					}
				  })
			},
			getScImg(filePaths){
				let imgList = [];
				filePaths.forEach((filePaths) => {
					const uploadTask = uni.uploadFile({
					  url: this.url+'/index.php/api/banner/upload', // 你的ThinkPHP服务器地址
					  filePath: filePaths,
					  name: 'file', // 这里根据服务器接收的字段来定义
					  success: (uploadFileRes) => {
						const data = JSON.parse(uploadFileRes.data);
						console.log('上传成功', data);
						// 这里处理返回的图片路径，例如保存到数组中
						imgList.push(data.filename);
						this.imagesList = imgList.join(',')
					  },
					  fail: (error) => {
						console.error('上传失败', error);
						uni.showToast({
							title: '上传失败',
							icon: 'none',
						});
					  }
					});
				});
				setTimeout(() => {
					this.getTsxx(this.imagesList);
				}, 2000);
			}
		},
	}
</script>

<style lang="scss" scoped>
	.cu-avatar {
		font-variant: small-caps;
		margin: 0;
		padding: 0;
		display: inline-flex;
		text-align: center;
		justify-content: center;
		align-items: center;
		background-color: #ccc;
		color: #ffffff;
		white-space: nowrap;
		position: relative;
		width: 64upx;
		height: 64upx;
		background-size: cover;
		background-position: center;
		vertical-align: middle;
		font-size: 1.5em;
	}
	
	.bg{
		width: 100%;
		height: 540rpx;
	}

	// 
	.publish {
		width: 100%;
		height: 100%;
		background: #fafbfb;

		.main_body {
			padding: 24rpx;
			background-color: #ffffff;
			width: 88%;
			margin: 24rpx auto 0;
			border: 1px solid #698375;

			.character {
				border-bottom: 2rpx solid #e8e8e8;
				padding: 22rpx 0;
			}

			.textarea {
				padding: 22rpx 0;
				border-bottom: 2rpx solid #e8e8e8;

				textarea {
					width: 100%;
					font-size: 28rpx;
				}
			}
		}

		.frequency {
			position: relative;
			width: 260rpx;
			height: 390rpx;

			.video {
				width: 260rpx !important;
				height: 390rpx !important;
				border-radius: 16rpx;

				.suspend {
					position: absolute;
					left: 106rpx;
					top: 162rpx;
					z-index: 100;
					width: 54rpx;
					height: 54rpx;
					border-radius: 100rpx;
				}

				.covers {
					position: absolute;
					width: 100%;
					bottom: 24rpx;
					z-index: 100;

					text {
						display: inline-block;
						width: 200rpx;
						height: 60rpx;
						line-height: 60rpx;
						font-size: 30rpx;
						color: #4D5EFF;
						background-color: #fff;
						border-radius: 8rpx;
					}
				}
			}

			.modal_box {
				position: absolute;
				background-color: rgba(0, 0, 0, 0.2);
				width: 260rpx;
				height: 390rpx;
				left: 0;
				top: 0;
				border-radius: 16rpx;
			}
		}


		.picture_list {
			display: flex;
			// flex-wrap: wrap;

			.delete {
				position: absolute;
				right: 5px;
				z-index: 10;
				top: 5px;
				width: 16px;
				height: 16px;
				border-radius: 2px;
			}

			view {
				width: 154rpx;
				margin-right: 15rpx;
				margin-bottom: 20rpx;
				height: 154rpx;
				position: relative;

				.cover {
					position: absolute;
					height: 44rpx;
					line-height: 44rpx;
					background: rgba(0, 0, 0, 0.4);
					bottom: 0;
					left: 0;
					font-size: 24rpx;
					margin: 0 !important;
					border-bottom-left-radius: 12rpx;
					border-bottom-right-radius: 12rpx;
				}

				image,
				.view {
					width: 154rpx;
					height: 154rpx;
					border-radius: 12rpx;
				}
			}
		}

		.delete_img {
			position: absolute;
			right: 10rpx;
			z-index: 10;
			top: 10rpx;
			width: 32rpx;
			height: 32rpx;
			border-radius: 4rpx;
		}

		.Upload_module {
			margin-top: 40rpx;

			.uploadImg {
				width: 150rpx;
				height: 150rpx;
				border: 2rpx solid #dfdfdf;
				border-radius: 12rpx;
				background: #fff;
				display: flex;
				align-items: center;
				justify-content: center;

				.image {
					display: flex;
					justify-content: center;
					align-items: center;

					image {
						margin-left: 16rpx;
						margin-top: 20rpx;
						width: 60rpx;
						height: 60rpx;
					}
				}
			}

			.layout {
				display: flex;
				align-items: center;
			}
		}



		.btn_bottom {
			height: 160rpx;
			padding: 12rpx 32rpx;
			background-color: #fafbfb;
			margin-top: 100rpx;

			.bnt1 {
				width: 100%;
				color: #fff;
				background: #4d5eff;
				height: 88rpx;
				line-height: 88rpx;
			}

			.bnt2 {
				width: 100%;
				height: 75rpx;
				line-height: 75rpx;
				background: #6a8473;
				color: #fff;
				font-weight: 300;
				font-size: 33rpx;
				border-radius: 0;
			}
		}

		.talking {
			padding: 24rpx;
			border-radius: 6rpx;
			margin-top: 10rpx;

			&>view:nth-child(1) {
				&>text:nth-child(1) {
					font-size: 28rpx;
					color: #3D3D3D;
				}

				&>text:nth-child(2) {
					font-size: 24rpx;
					background-color: #e9eafb;
					color: #4d5eff;
					margin-left: 24rpx;
					padding: 4rpx 6rpx;
					border-radius: 12rpx;
				}
			}

			&>view:nth-child(2) {
				background: #fff;
				padding: 24rpx;
				margin-top: 16rpx;

				&>text {
					margin-right: 16rpx;
					color: #000;
				}
			}
		}

	}
</style>